<template>
  <view>
    <nav-bar :title="navBarTitle" />
    <!--    <view-->
    <!--      class="wifi-tip"-->
    <!--      v-if="deviceSystem === 'ios' && isLoading === WIFI_STATE.WAITING"-->
    <!--      >苹果要到设置，点击跳转-->
    <!--      <u-button @click="handleSearchNearByWIFI">跳转</u-button>-->
    <!--    </view>-->
    <view class="container">
      <!--      <view class="loading" v-if="isLoading === WIFI_STATE.TESTING">-->
      <!--        <u-loading-icon text="WiFi搜索中" textSize="18"></u-loading-icon>-->
      <!--      </view>-->
      <view class="list">
        <block v-if="list.length > 0">
          <view
            class="list-item flex-align-center"
            v-for="(item, index) in list"
            :key="index"
            @click="handleLink(item)"
          >
            <view class="wifi-name">{{ item.wifiName }}</view>
            <!--            <view class="link" @click="handleLink(item)"-->
            <!--              >强度{{ item.signalStrength }}</view-->
            <!--            >-->
          </view>
        </block>
        <view v-else class="empty">
          <u-empty
            text="附近没有可用WiFi"
            mode="wifi"
            icon="http://cdn.uviewui.com/uview/empty/wifi.png"
          />
        </view>
      </view>
    </view>
  </view>
</template>

<script>
// import wifiSignal from "@/components/pageComponents/sp-common/wifiSignal";
import { WIFI_STATE } from "@/pages/sp-store/const";
import { getShopWifiListByPubShopId } from "@/service/api/sp-store/wifi";
export default {
  components: {},
  props: {
    show: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      navBarTitle: "门店WiFi",
      list: [],
      WIFI_STATE,
      isLoading: WIFI_STATE.WAITING,
      // nearByWiFiList: [],
      // originNearByWiFiList: [],
      // deviceSystem: "",
    };
  },
  onLoad() {
    this.fetch();
  },
  methods: {
    fetch() {
      getShopWifiListByPubShopId({
        pubDomainShopId: this.$Route.query.shopId,
      }).then((res) => {
        console.log("门店WiFi", res);
        if (!res) return;
        this.list = res;
        this.isLoading = WIFI_STATE.SUCCESS;

        // const system = uni.getSystemInfoSync();
        // if (system?.osName === "android") {
        //   this.$nextTick(() => {
        //     this.handleSearchNearByWIFI();
        //   });
        // }
        // if (system.osName === "ios") {
        //   //打开WiFi列表
        // }
        // this.deviceSystem = system.osName;
      });
    },
    handleLink(item) {
      // uni.navigateTo({
      //   url: `pages/sp-store/pages/link-wifi/index?name=${item.wifiName}&password=${item.password}&deviceNo=${item.wifiCode}`,
      // });
      this.$Router.push({
        name: "linkWifi",
        params: {
          name: item.wifiName,
          password: item.password,
          deviceNo: item.wifiCode,
        },
      });
    },
    // handleSearchNearByWIFI() {
    //   this.isLoading = WIFI_STATE.TESTING;
    //   uni.startWifi({
    //     success: () => {
    //       uni.authorize({
    //         scope: "scope.userLocation",
    //         success: () => {
    //           uni.getWifiList({
    //             success: () => {
    //               uni.onGetWifiList((l) => {
    //                 const list = l.wifiList;
    //                 this.originNearByWiFiList = this.handleFilterWiFi(list);
    //                 this.$nextTick(() => {
    //                   //将本地WiFi和接口WiFi做ssid对照
    //                   this.handleCompareWiFi();
    //                 });
    //                 console.log("WiFi列表", list);
    //               });
    //             },
    //           });
    //         },
    //         fail: () => {
    //           this.isLoading = WIFI_STATE.WIFI_FAIL;
    //           uni.showToast({
    //             title: "请检查WiFi设置",
    //             icon: "error",
    //           });
    //         },
    //       });
    //     },
    //     fail: () => {
    //       this.isLoading = WIFI_STATE.WIFI_FAIL;
    //       uni.showToast({
    //         title: "请检查WiFi设置",
    //         icon: "error",
    //       });
    //     },
    //   });
    // },
    // handleFilterWiFi(arr) {
    //   if (!Array.isArray(arr)) {
    //     console.log("type error!");
    //     return;
    //   }
    //   let array = [];
    //   for (let i = 0; i < arr.length; i++) {
    //     if (!(array.filter((v) => v.SSID === arr[i].SSID).length > 0)) {
    //       //includes 检测数组是否有某个值
    //       array.push(arr[i]);
    //     }
    //   }
    //   return array;
    // },
    // handleCompareWiFi() {
    //   this.nearByWiFiList = [];
    //   this.list.map((v) => {
    //     const arr = this.originNearByWiFiList.filter(
    //       (o) => o.SSID === v.wifiName
    //     );
    //     v.signalStrength =
    //       (this.deviceSystem === "ios"
    //         ? arr[0]?.signalStrength * 100
    //         : arr[0]?.signalStrength) || 0;
    //     this.nearByWiFiList.push(v);
    //   });
    //   this.isLoading = WIFI_STATE.SUCCESS;
    //   // setTimeout(() => {
    //   //   this.fetch();
    //   // }, 5000);
    // },
  },
};
</script>

<style scoped lang="scss">
.container {
  background-color: #f7f7f7;
  min-height: 100vh;
}
.title {
  text-align: center;
  padding: 10rpx 0;
}
.close-icon {
  position: fixed;
  bottom: 480rpx;
  left: 50%;
  transform: translateX(-50%);
}
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 60rpx;
  padding-bottom: 60rpx;
  width: 90vw;
  height: 300rpx;
}
.empty {
  margin-top: 60rpx;
}
.list {
  width: 90vw;
  height: 300rpx;
  margin: 0 auto;
  padding-bottom: 60rpx;
  padding-top: 20rpx;
  border-radius: 18rpx;
  &-item {
    padding: 30rpx 33rpx;
    border-bottom: 1rpx solid #ebebeb;
    background-color: white;
    .wifi-name {
      flex: 1;
      font-size: 1rem;
      //text-align: center;
    }
    .link {
    }
  }
}
</style>
